package tongwentongshu.com.app.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import butterknife.BindView;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.db.Cache;

/**
 * Created by XiongBin on 2017/7/28.
 */

public class ShareCompleteActivity extends BaseActivity {
    @BindView(R.id.gv)
    GridView gv;
    @BindView(R.id.title_complete)
    TextView complete;

    PoliteAdapter adapter;

    @Override
    protected int getLayoutResourceId() {
        return R.layout.activity_share_complete;
    }

    @Override
    protected int getTitleId() {
        return R.string.share_complete;
    }

    @Override
    protected void initView() {
        complete.setVisibility(View.VISIBLE);
        complete.setText("完成");
        adapter = new PoliteAdapter();
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                showShare(share[position]);
            }
        });
    }

    @Override
    protected void initData() {
        complete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    public String getBookId() {
        return getIntent().getExtras().getString("bookid");
    }

    public String getImg() {
        return getIntent().getExtras().getString("img");
    }

    public String getBookName() {
        return getIntent().getExtras().getString("bookname");
    }

    String[] shareName = {"微信好友", "微信朋友圈", "QQ空间", "QQ好友", "微博"};
    String[] share = {Wechat.NAME, WechatMoments.NAME, QZone.NAME, QQ.NAME, SinaWeibo.NAME};
    int[] sharePic = {R.drawable.weixinq, R.drawable.weixinp, R.drawable.qqz, R.drawable.qq, R.drawable.weibo};

    class PoliteAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return 4;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder vh = null;
            if (convertView == null) {
                vh = new ViewHolder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.view_polite, null);
                vh.text = (TextView) convertView.findViewById(R.id.tv_text);
                vh.iv = (ImageView) convertView.findViewById(R.id.iv_pic);
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            vh.text.setText(shareName[position]);
            vh.iv.setImageResource(sharePic[position]);
            return convertView;
        }

        class ViewHolder {
            TextView text;
            ImageView iv;

        }
    }

    private void showShare(String name) {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setPlatform(name);
        // 分享时Notification的图标和文字  2.5.9以后的版本不     调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("看有力量的书籍，交有阅历的朋友");
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl(Cache.getUser().getData().getShare_book() + getBookId());
        // text是分享文本，所有平台都需要这个字段
        oks.setText("您的好友分享了一本图书《" + getBookName() + "》,快来阅读吧~");
        // imagePath是图片的本地 路径，Linked-In以外的平台都支持此参数
//        oks.setImagePath("http://img10.360buyimg.com/imgzone/jfs/t2470/202/1126456897/30846/e9a0697a/5641aa68Nde8c0189.jpg");//确保SDcard下面存在此张图片
        oks.setImageUrl(getImg());
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl(Cache.getUser().getData().getShare_book() + getBookId());
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
//        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(getString(R.string.app_name));
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(Cache.getUser().getData().getShare_book() + getBookId());

        // 启动分享GUI
        oks.show(this);
    }
}
